<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\SettingUserLevel;
use App\Model\SettingUserTags;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class UserLevelController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('用户等级管理');
        $content->body($this->grid());
        return $content;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('等级信息编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('等级创建');
        $content->body($this->form());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new SettingUserLevel());
        $grid->model()->orderBy('level_type', 'desc')->orderBy('level', 'desc');
        $grid->level('等级')->sortable();
        $grid->level_type('等级分类')->display(function ($data) {
            return SettingUserLevel::TYPE[$data];
        });
        $grid->title('等级名');
        $grid->number('等级阈值')->editable();
        $grid->chat_coin_setting('私聊收费（最高）')->editable();
        $grid->voice_coin_setting('语音通话收费（最高）')->editable();
        $grid->video_coin_setting('视频通话收费（最高）')->editable();
        $grid->flag('状态')->display(function ($data) {
            return $data == 1 ? '有效' : '无效';
        });
        //禁用导出
        $grid->disableExport();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('level_type', '等级类型')->select(SettingUserLevel::TYPE);
            });
        });
        //禁用行选择
        $grid->disableRowSelector();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new SettingUserLevel());
        $form->select('level', '等级')->options(SettingUserLevel::getLevels(30))->default(1)->required();
        $form->text('title', '等级名');
        $form->select('level_type', '类别')->options(SettingUserLevel::TYPE)->default(1)->required();
        $form->text('number', '魅力值（或豪气值）阈值')->default(0)->required();
        $form->text('chat_coin_setting', '私聊收费（最高）')->default(0.0);
        $form->text('voice_coin_setting', '语音通话收费（最高）')->default(0.0);
        $form->text('video_coin_setting', '视频通话收费（最高）')->default(0.0);
        $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1)->required();
        $form->saving(function (Form $form) {
        });
        $form->saved(function (Form $form) {
        });
        return $form;
    }
}
